package 打牢基础30天.day01.my;

/**
 * @Author yaya
 * @Create 2024/6/4 15:30
 * Description:二分查找是否存在某个数
 */
public class Code04_BinarySearchExist {


    public static int binarySearchExist(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;

        while (left <= right){
            mid = left +((right - left) >> 1);
            if (arr[mid] == target) {
                return mid;
            }else if (target < arr[mid]) {
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,6,8,9,10,35,77,112};
        for (int i : arr) {
            System.out.println(binarySearchExist(arr,i));
        }
    }
}
